import axios from 'axios';
import { message } from 'ant-design-vue';
import { getToken } from '../utils/auth'
import Config from '../config'

const service = axios.create({
    baseURL: '',
    timeout: Config.timeout // 请求超时时间
});

// 添加请求拦截器
service.interceptors.request.use(
    config => {
        if (getToken()) {
            config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
        }
        config.headers['Content-Type'] = 'application/json'
        return config
    },
    error => {
        // Do something with request error
        message.info(error);
        Promise.reject(error)
    }
);

service.interceptors.response.use(
    response => {
        const code = response.status
        if (code < 200 || code > 300) {
            message.info("网络请求错误");
            return Promise.reject('error')
        } else {
            return response.data
        }
    },
    error => {
        console.log(error);
    }
);



export default service;